package com.kitty.game.pet.handler.upgrade;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.pet.message.RespPetEclosionResult;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.Strings;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import static com.kitty.game.utils.Const.ROLE_RESOURCE_NOT_ENOUGH;

@Component
public class EclosionPetHandler extends UpgradePetHandler {
    private static final String SUCCESS_MSG = "恭喜，你的#Y{0}#n获得#R{1}#n灵气，当前羽化进度{2}#R{3}%#n。";
    private static final String ERROR_MSG = "该宠物已经完成羽化，无法提升灵气。";
    private static final String ECLOSION_DONE = "恭喜，您的宠物羽化完成，得成正果。";
    private static final String ECLOSION_ONE_DONE = "恭喜，你的#Y{0}#n获得#R{1}#n灵气，当前羽化进度{2}#R{3}%#n，{4}羽化完成。";
    private static final String ECLOSION_ALL_DONE = "恭喜#Y{0}#n的宠物#Y{1}#n完成了所有阶段羽化，实力更上一层楼。";


    @Override
    public String acceptUpgradePet(Role role, Pet pet, String ids, String itemPos, String costType, String otherPetPos) {
        if (pet.getEnchant() != 2) {
            return NOT_ENCHANT;
        }
        if (pet.getEclosion() == 2) {
            return ERROR_MSG;
        }
        if (pet.getEclosion() <= 0) {
            return NOT_OPEN_ECLOSION;
        }
        int limitNum = 0;//限制物品数量
        int totalNimbus;
        /**没有物品代表是一键羽化 判断元宝*/
        if (Strings.isEmpty(itemPos)) {
            int roleGold = role.getGold();
            int roleSilver = role.getSiver();
            if (Strings.isEmpty(costType)) {//银元宝
                if (roleSilver < 3108) {
                    if (roleGold < 3108) {
                        return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, "元宝");
                    }
                    SpringUtils.getRoleService().subtractGold(role, 3108, Reason.ECLOSION_PET);
                }
                limitNum = 6;
                roleService.subtractSiver(role, 3108, Reason.ECLOSION_PET);
            } else {//金元宝
                if (roleGold < 3108) {
                    return MessageFormat.format(ROLE_RESOURCE_NOT_ENOUGH, "元宝");
                }
                SpringUtils.getRoleService().subtractGold(role, 3108, Reason.ECLOSION_PET);
            }
            roleService.updateRoleGoldAndSiver(role);
            totalNimbus = 3000 * 6;
        } else {
            List<RoleEquip> equips = new ArrayList<>();
            for (String ss : itemPos.split("\\|")) {
                RoleEquip roleEquip = equipService.getRoleEquipByPos(role, Short.parseShort(ss));
                if (roleEquip == null) {
                    continue;
                }
                equips.add(roleEquip);
                if (roleEquip.isLimit()) {
                    limitNum++;
                }
            }
            totalNimbus = getEclosionNimbusByItems(role, equips);
        }
        petService.addLimitTime(pet, limitNum);
        pet.setEclosion_v(pet.getEclosion_v() + totalNimbus);
        String percentMsg = "%.2f";
        double percent = (double) pet.getEclosion_v() / (double) pet.getEclosion_k();
        percentMsg = String.format(percentMsg, (percent * 100));
        String resultMessage;
        if (pet.getEclosion_v() >= pet.getEclosion_k()) {
            setEclosionValue(pet);
            if (pet.getEclosion_rank() >= 2) {
                pet.setEclosion_v(0);
                pet.setEclosion_k(0);
                pet.setEclosion((byte) 2);
                resultMessage = ECLOSION_DONE;
                SpringUtils.getBroadcastService().sendRumor(MessageFormat.format(ECLOSION_ALL_DONE, role.getName(), pet.getName()), null);
            } else {
                pet.setEclosion_rank((byte) (pet.getEclosion_rank() + 1));
                pet.setEclosion_v(0);
                int maxNimbus = getEclosionMaxNimbus(pet);
                pet.setEclosion_k(maxNimbus);
                resultMessage = MessageFormat.format(ECLOSION_ONE_DONE, pet.getName(), String.valueOf(totalNimbus), ECLOSION_RANK.get(pet.getEclosion_rank()-1), percentMsg,ECLOSION_RANK.get(pet.getEclosion_rank()-1));
            }
        } else {
            resultMessage = MessageFormat.format(SUCCESS_MSG, pet.getName(), String.valueOf(totalNimbus), ECLOSION_RANK.get(pet.getEclosion_rank()), percentMsg);
        }

        petService.refreshPetInfo(role, pet);
        refreshPetField(role, pet);

        RespPetEclosionResult RespPetEclosionResult = new RespPetEclosionResult();
        RespPetEclosionResult.setResult((byte) 1);
        MessagePusher.pushMessage(role, RespPetEclosionResult);
        return resultMessage;
    }


}
